package com.googlecode.perfectable.persistable.session;

import java.util.UUID;

import javax.annotation.Nullable;

import com.google.common.base.Objects;

public class UniversalIdentifier<T> implements Identifier<T> {
	private static final long serialVersionUID = -2110185340606500979L;

	public static final IdentifierGenerator GENERATOR = new IdentifierGenerator() {
		@Override
		public <T> Identifier<T> generate() {
			return new UniversalIdentifier<>();
		}
	};

	private final UUID value;

	UniversalIdentifier() {
		this.value = UUID.randomUUID();
	}

	@Override
	public String code() {
		return this.value.toString();
	}

	@Override
	public boolean equals(@Nullable Object obj) {
		if(this == obj) {
			return true;
		}
		if(!(obj instanceof UniversalIdentifier)) {
			return false;
		}
		UniversalIdentifier<?> other = (UniversalIdentifier<?>) obj;
		return Objects.equal(this.value, other.value);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.value);
	}

}
